from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import views
from .views import AuthorNovelViewSet, CategoryListAPIView, AuthorWorksViewSet

router = DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'novels', views.NovelViewSet, basename='novel')
router.register(r'chapters', views.ChapterViewSet, basename='chapter')
router.register(r'bookshelf', views.BookshelfViewSet, basename='bookshelf')
router.register(r'admin-api', views.AdminViewSet, basename='admin-api')
router.register(r'categories', views.CategoryViewSet, basename='category')
router.register(r'author-works', AuthorWorksViewSet, basename='author-works')

urlpatterns = [
    # Web视图
    path('', views.frontend_home, name='frontend_home'),  # 前端首页
    path('login/', views.custom_login, name='login'),  # 自定义登录
    path('register/', views.register, name='register'),  # 注册
    path('logout/', views.custom_logout, name='logout'),  # 登出

    # 用户中心
    path('user/profile/', views.user_profile, name='user_profile'),  # 个人中心
    path('user/change-password/', views.change_password, name='change_password'),  # 修改密码
    path('user/bookshelf/', views.bookshelf, name='bookshelf'),  # 我的书架
    path('user/author-apply/', views.author_apply, name='author_apply'),  # 申请成为作者
    path('api/author/novels/<int:pk>/submit/',
         AuthorNovelViewSet.as_view({'patch': 'submit_review'}),
         name='novel-submit'),
    path('author-center/', views.author_center_view, name='author_center'),  # 作者中心

    # 小说相关页面
    path('novels/', views.novel_list, name='novel_list'),  # 小说列表
    path('novels/search/', views.novel_search, name='novel_search'),  # 搜索结果
    path('novels/<int:novel_id>/', views.novel_detail, name='novel_detail'),  # 小说详情
    path('novels/<int:novel_id>/read/', views.read_novel, name='read_novel'),  # 开始阅读（重定向到第一章）
    path('novels/<int:novel_id>/read/<int:chapter_id>/', views.read_chapter, name='read_chapter'),  # 章节阅读
    path('api/categories/', CategoryListAPIView.as_view(), name='api_categories'),

    # AJAX API
    path('api/bookshelf/add/', views.add_to_bookshelf, name='add_to_bookshelf'),  # 添加到书架
    path('api/bookshelf/remove/', views.remove_from_bookshelf, name='remove_from_bookshelf'),  # 从书架移除
    path('api/chapters/<int:chapter_id>/read/', views.update_reading_progress, name='update_reading_progress'),
    # 更新阅读进度

    # REST API路由
    path('api/', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
